package vip.web3.admin.sys.controller.biz;

import com.alibaba.excel.EasyExcel;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import vip.web3.admin.sys.controller.base.AbstractController;
import vip.web3.admin.sys.entity.biz.AUserEntity;
import vip.web3.admin.sys.form.user.AuthAuditForm;
import vip.web3.admin.sys.form.user.AuthPageForm;
import vip.web3.admin.sys.form.user.UserPageForm;
import vip.web3.admin.sys.service.biz.IAUserService;
import vip.web3.admin.sys.vo.biz.AuthVo;
import vip.web3.admin.sys.vo.biz.export.ExUserVo;
import vip.web3.common.exception.RRException;
import vip.web3.common.utils.DateUtils;
import vip.web3.common.utils.PageUtils;
import vip.web3.common.utils.R;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * aut-horlilei
 */
@RestController
@RequestMapping("/sys/user")
public class AUserController extends AbstractController {


    @Autowired
    private IAUserService aUserService;

    @GetMapping("/list")
    @RequiresPermissions("sys:user:list")
    public R listUser(@RequestParam Map<String, Object> params, @Valid UserPageForm form) {
        PageUtils page = aUserService.listUser(params, form);
        return R.ok().put("page", page);
    }

    @RequiresPermissions("sys:user:list")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam Map<String, Object> params,
                       @Valid UserPageForm form) throws IOException {
        List<ExUserVo> orders = aUserService.export(params, form);
        if (CollectionUtils.isEmpty(orders)) {
            orders = Collections.EMPTY_LIST;
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("用户数据_" + DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ExUserVo.class).sheet("用户").doWrite(orders);

    }

    @GetMapping("/{id}")
    public R get(@PathVariable Integer id) {
        return R.ok().put("data", aUserService.getById(id));
    }

    @PostMapping("/edit")
    @RequiresPermissions("sys:user:update")
    public R edit(@RequestBody AUserEntity form) {
        aUserService.edit(form);
        return R.ok();
    }

    @PostMapping("/editPwd")
    @RequiresPermissions("sys:user:update")
    public R editPwd(@RequestBody AUserEntity form) {
        if (Objects.isNull(form) || form.getId() == null) {
            throw new RRException("id不能为空!!", -1);
        }
        if (StringUtils.isBlank(form.getPassword())) {
            throw new RRException("密码不能为空!!", -1);
        }
        AUserEntity entity = new AUserEntity();
        entity.setId(form.getId());
        entity.setPassword(DigestUtils.sha256Hex(form.getPassword()));
        this.aUserService.updateById(entity);
        return R.ok();
    }

    @GetMapping("/authInfo/list")
    @RequiresPermissions("sys:userauth:list")
    public R listAuthInfo(@RequestParam Map<String, Object> params, @Valid AuthPageForm form) {
        PageUtils page = aUserService.pageAuthInfo(params, form);
        return R.ok().put("page", page);
    }

    @GetMapping("/authInfo/{id}")
    @RequiresPermissions("sys:userauth:info")
    public R getAuthInfo(@PathVariable Integer id) {
        AuthVo authVo = aUserService.getAuthInfo(id);
        return R.ok().put("data", authVo);
    }

    @PostMapping("/authAudit")
    @RequiresPermissions("sys:userauth:update")
    public R authAudit(@Valid @RequestBody AuthAuditForm form) {
        aUserService.authAudit(form);
        return R.ok();
    }

    @PostMapping("/pass/auth")
    @RequiresPermissions("sys:userauth:update")
    public R passAuth(@RequestBody List<Integer> ids) {
        aUserService.passAuth(ids);
        return R.ok();
    }

    @PostMapping("/pass/off")
    @RequiresPermissions("sys:userauth:update")
    public R off(@RequestBody List<Integer> ids) {
        aUserService.statusOff(ids);
        return R.ok();
    }
}
